package com.sojson.project.pay.controller.ali;

import static com.sojson.util.pay.ali.constant.AliConstant.FAIL_CALL_BACK;
import static com.sojson.util.pay.ali.constant.AliConstant.SUCCESS_CALL_BACK;
import static com.sojson.util.pay.ali.constant.AliConstant.TRADE_STATUS_VALUE_TRADE_SUCCESS;

import java.io.IOException;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sojson.project.dao.OrderChildDao;
import com.sojson.project.dao.OrderDao;
import com.sojson.project.dao.OrderDepositDao;
import com.sojson.project.pay.controller.ali.param.verify.PayAliParamVerify;
import com.sojson.project.pay.service.ali.NotifyPayAliService;
import com.sojson.util.StringUtil;
import com.sojson.util.pay.ali.AliUtil;
import com.sojson.util.pay.ali.bean.AliResponse;
import com.sojson.util.pay.ali.enums.EAliCallBackType;

import io.swagger.annotations.Api;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 支付宝支付回调控制器
 * 
 * @author liu
 * @date 2021-03-24
 */
@Api(tags = "支付宝支付回调管理")
@ApiIgnore
// @RestController
@RequestMapping(value = "/notify/pay/ali")
public class NotifyPayAliController {

    @Autowired
    private NotifyPayAliService notifyPayAliService;
    @Resource
    private OrderDao orderDao;
    @Resource
    private OrderChildDao orderChildDao;
    @Resource
    private OrderDepositDao orderDepositDao;

    /**
     * 回调(商城)
     * 
     * @return
     */
    @RequestMapping("/goods")
    public String notifyPay() {
        try {
            // 校验参数
            AliResponse paramResult = PayAliParamVerify.payNotify();
            // 获取回调类型
            int callBack = AliUtil.getCallBack(paramResult);

            // 支付
            if (callBack == EAliCallBackType.PAY.getCode()) {
                // 处理订单
                boolean result = notifyPayAliService.goods(paramResult);
                if (result) {
                    return SUCCESS_CALL_BACK;
                } else {
                    return FAIL_CALL_BACK;
                }
            }

            // 退款
            if (callBack == EAliCallBackType.RETURN.getCode()) {
                // 处理订单
                boolean result = notifyPayAliService.goodsCancel(paramResult);
                if (result) {
                    return SUCCESS_CALL_BACK;
                } else {
                    return FAIL_CALL_BACK;
                }
            }
        } catch (Exception e) {
            // 验证不通过
            return FAIL_CALL_BACK;
        }

        // 通知支付宝处理完成
        return SUCCESS_CALL_BACK;
    }

    /**
     * 回调(诚意金)
     * 
     * @return
     */
    @RequestMapping("/deposit")
    public String deposit() throws IOException {
        try {
            // 校验参数
            AliResponse paramResult = PayAliParamVerify.payNotify();
            // 获取回调类型
            int callBack = AliUtil.getCallBack(paramResult);

            // 支付
            if (callBack == EAliCallBackType.PAY.getCode()) {
                // 处理订单
                boolean result = notifyPayAliService.deposit(paramResult);
                if (result) {
                    return SUCCESS_CALL_BACK;
                } else {
                    return FAIL_CALL_BACK;
                }
            }

            // 退款
            if (callBack == EAliCallBackType.RETURN.getCode()) {
                // 处理订单
                boolean result = notifyPayAliService.depositCancel(paramResult);
                if (result) {
                    return SUCCESS_CALL_BACK;
                } else {
                    return FAIL_CALL_BACK;
                }
            }
        } catch (Exception e) {
            // 验证不通过
            return FAIL_CALL_BACK;
        }

        // 通知支付宝处理完成
        return SUCCESS_CALL_BACK;
    }

    /**
     * 支付回调
     * 
     * @return
     */
    @RequestMapping("/notifyPayGoOn")
    public String notifyPayGoOn() {
        try {
            // 校验参数并解密密文
            AliResponse paramResult = PayAliParamVerify.payNotify();
            // 验证不通过
            if (StringUtil.isBlankObject(paramResult) || StringUtil.isBlankObject(paramResult.getOutTradeNo())) {
                return FAIL_CALL_BACK;
            }
            String tradeStatus = paramResult.getTradeStatus();
            if (StringUtil.isNotBlankObject(tradeStatus)
                && !paramResult.getTradeStatus().equals(TRADE_STATUS_VALUE_TRADE_SUCCESS)) {
                // 通知支付宝处理完成
                return SUCCESS_CALL_BACK;
            }

            // 处理订单
            boolean notifyPay = notifyPayAliService.notifyPayGoOn(paramResult);
            if (!notifyPay) {
                return FAIL_CALL_BACK;
            }
        } catch (Exception e) {
            // 验证不通过
            return FAIL_CALL_BACK;
        }

        // 通知支付宝处理完成
        return SUCCESS_CALL_BACK;
    }

    /**
     * 退款回调
     * 
     * @return
     */
    @RequestMapping("/notifyPayCancel")
    public String notifyPayCancel() {
        try {
            // 校验参数并解密密文
            AliResponse paramResult = PayAliParamVerify.payNotify();
            // 验证不通过
            if (StringUtil.isBlankObject(paramResult) || StringUtil.isBlankObject(paramResult.getOutTradeNo())) {
                return FAIL_CALL_BACK;
            }
            String tradeStatus = paramResult.getTradeStatus();
            if (StringUtil.isNotBlankObject(tradeStatus)
                && !paramResult.getTradeStatus().equals(TRADE_STATUS_VALUE_TRADE_SUCCESS)) {
                // 通知支付宝处理完成
                return SUCCESS_CALL_BACK;
            }

            // 处理订单
            boolean notifyPayCancel = notifyPayAliService.goodsCancel(paramResult);
            if (!notifyPayCancel) {
                return FAIL_CALL_BACK;
            }
        } catch (Exception e) {
            // 验证不通过
            return FAIL_CALL_BACK;
        }

        // 通知支付宝处理完成
        return SUCCESS_CALL_BACK;
    }

    /**
     * 主动退款回调
     * 
     * @return
     */
    @RequestMapping("/notifyPayCancelInitiative")
    public String notifyPayCancelInitiative() {
        try {
            // 校验参数并解密密文
            AliResponse paramResult = PayAliParamVerify.payNotify();
            // 验证不通过
            if (StringUtil.isBlankObject(paramResult) || StringUtil.isBlankObject(paramResult.getOutTradeNo())) {
                return FAIL_CALL_BACK;
            }
            String tradeStatus = paramResult.getTradeStatus();
            if (StringUtil.isNotBlankObject(tradeStatus)
                && !paramResult.getTradeStatus().equals(TRADE_STATUS_VALUE_TRADE_SUCCESS)) {
                // 通知支付宝处理完成
                return SUCCESS_CALL_BACK;
            }

            // 处理订单
            boolean notifyPayCancel = notifyPayAliService.notifyPayCancelInitiative(paramResult);
            if (!notifyPayCancel) {
                return FAIL_CALL_BACK;
            }
        } catch (Exception e) {
            // 验证不通过
            return FAIL_CALL_BACK;
        }

        // 通知支付宝处理完成
        return SUCCESS_CALL_BACK;
    }

    /**
     * 子订单主动退款回调
     * 
     * @return
     */
    @RequestMapping("/notifyPayCancelGoOnInitiative")
    public String notifyPayCancelGoOnInitiative() {
        try {
            // 校验参数并解密密文
            AliResponse paramResult = PayAliParamVerify.payNotify();
            // 验证不通过
            if (StringUtil.isBlankObject(paramResult) || StringUtil.isBlankObject(paramResult.getOutTradeNo())) {
                return FAIL_CALL_BACK;
            }
            String tradeStatus = paramResult.getTradeStatus();
            if (StringUtil.isNotBlankObject(tradeStatus)
                && !paramResult.getTradeStatus().equals(TRADE_STATUS_VALUE_TRADE_SUCCESS)) {
                // 通知支付宝处理完成
                return SUCCESS_CALL_BACK;
            }

            // 处理订单
            boolean notifyPayCancel = notifyPayAliService.notifyPayCancelGoOnInitiative(paramResult);
            if (!notifyPayCancel) {
                return FAIL_CALL_BACK;
            }
        } catch (Exception e) {
            // 验证不通过
            return FAIL_CALL_BACK;
        }

        // 通知支付宝处理完成
        return SUCCESS_CALL_BACK;
    }

}